Page({
  data: {
    username: '',
    password: ''
  },
  inputUsername:function(e){
    console.log(e.detail.value);
    this.setData({username:e.detail.value});
  },
  inputPassword:function(e){
    console.log(e.detail.value);
    this.setData({
      password:e.detail.value,
    });
  },
  goTo:function(){
    setTimeout(()=>{
      wx.navigateTo({
        url: '/pages/register/register',
      });
    },1000);
  },
  login: function() {
    console.log(this.data);
    if(!this.data.username||!this.data.password){
      wx.showToast({
        title: '账号或密码为空',
        icon:'error',
        duration:2000
      });
      return;
    }
    wx.request({
      url:  `http://81.70.199.174:8081/user/login?username=${this.data.username}&password=${this.data.password}`,
      method:'POST',
      
      success:(res)=>{
        console.log(res);
        if(res.data.code===200){
          wx.showToast({
            title:res.data.message,
            icon:'success',
            duration:2000
          });
          const app=getApp();
          app.globalData.username1=this.data.username;
          app.globalData.token = res.data.data;
          setTimeout(()=>{
          wx.switchTab({
            url:"/pages/main2/main2",
            success: function(res) {
              console.log("跳转成功", res);
            },
            fail: function(err) {
              console.error("跳转失败", err);
            }
          });
          },2000);
          console.log(getApp().globalData.username1);
        }else if(res.data.code===1002){
          wx.showToast({
            title: res.data.message,
            icon:'error',
            duration:2000
          });
        }else{
          wx.showToast({
            title:res.data.message,
            icon:'error',
            duration:2000
          });
        }
      },
      fail(error){
        console.log(error);
        wx.showToast({
          title: '登录失败',
          icon:'error',
          duration:2000
        });
      }
    })
  },
});